import React from "react"
import {compose, lifecycle, withHandlers, withState} from "recompose"
import {del, get, putParam} from "server"
import {pageSetting} from "project-config"
import {message, Modal} from "antd"
import {ExclamationCircleOutlined} from '@ant-design/icons'

const messageCompose = compose(
  withState('dataSource', '$dataSource', []),
  withState('loading', '$loading', false),
  withState('selectedRowKeys', '$selectedRowKeys', []),
  withState('pagination', '$pagination', Object.assign({}, pageSetting)),
  withState('page', '$page', {}),
  withState('filters', '$filters', {}),
  withState('searchKey', '$searchKey', ''),
  withHandlers({
    getPageData: ({$pagination, pagination, $loading, $dataSource, $selectedRowKeys}) => {
      return (param, pageSize, pageNum) => {
        $loading(true)
        get(`/message/myMessages/${pageSize || 10}/${pageNum || 1}`, param)
          .then(page => {
            $dataSource(page.pageData)
            $selectedRowKeys([])
            pagination.total = page.total
            pagination.pageSize = pageSize || 10
            pagination.current = pageNum || 1
            $pagination(Object.assign({}, pagination))
          }).finally(() => {
          $loading(false)
        })
      }
    },
  }),
  withHandlers({
    onFastSearch: ({$searchKey, getPageData}) => {
      return (key) => {
        $searchKey(key)
        getPageData({orderNo: key})
      }
    }
  }),
  withHandlers({
    onPageChange: ({searchKey, getPageData, $filters, $page}) => {
      return (page, filters) => {
        const param = {orderNo: searchKey || ''}
        if (filters && filters['status']) {
          $filters(filters)
          $page(page)
          param['statusList'] = filters['status']
        }
        getPageData(param, page.pageSize, page.current)
      }
    },
    deleteBatch: ({selectedRowKeys, getPageData, searchKey}) => {
      return () => {
        const action = () => {
          del(`/message/myMessage/batch`, {ids: selectedRowKeys})
            .then((data) => {
              getPageData(searchKey ? {code: searchKey} : {})
              message.success(`成功删除${data}条记录`)
            })
        }

        Modal.confirm({
          icon: <ExclamationCircleOutlined />,
          title: '确认取消',
          content: <div>确认要删除以下 <b className={'line-stress'}>{selectedRowKeys.length}</b> 项消息吗?</div>,
          onOk: action
        })
      }
    },
    onDelete: ({getPageData, searchKey, filters, page}) => {
      return (row) => {
        del(`/message/myMessage/${row.id}`)
          .then(() => {
            getPageData()
            message.success(`消息: ${row.title} 已成功取消!`)
          })
      }
    },
    onRead: ({getPageData}) => {
      return (row) => {
        putParam(`/message/myMessage/read/${row.id}`)
          .then(() => {
            getPageData()
          })
      }
    }
  }),
  lifecycle({
    componentDidMount() {
      const {getPageData} = this.props
      getPageData()
    }
  })
)

export default messageCompose